#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
@AUTHOR: tyc
@VERSION: 1.0.0
@DESCRIPTION: 搜索超级鹰使用这个来识别验证码 直接下载python demo代码测试即可
# 登录12306
@time: 2021-07-13 14:11:52
"""
from time import sleep
from selenium.webdriver import Chrome
from chaojiying import Chaojiying_Client
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options


login_url = 'https://kyfw.12306.cn/otn/resources/login.html'
user_name = '18238827823'
pass_word = '147852'
# 超级鹰软件id
software_id = '919585'

# 避免监测到非人为打开的浏览器
opt = Options()
opt.add_argument('--disable-blink-features=AutomationControlled')
browser = Chrome(options=opt)
browser.get(login_url)


sleep(2)
# 点击账号密码登录
browser.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click()
# 获取验证码图片
sleep(2)
vertify_img_el = browser.find_element_by_xpath('//*[@id="J-loginImg"]')
vertify_img = vertify_img_el.screenshot_as_png
chaojiying = Chaojiying_Client(user_name, pass_word, software_id)
# 获取到验证码
result = chaojiying.PostPic(vertify_img, 9004)
print(f'验证码识别结果：{result}')
vertify_code = result['pic_str'] # 获取到需要点击的坐标 175,139|41,135
point_list = vertify_code.split('|')
print(vertify_code)
for point in point_list:
    p = point.split(',')
    x = int(p[0])
    print(x)
    y = int(p[1])
    print(y)
    # 移动鼠标到验证码图片 然后点击坐标 不一定每次都能正确
    ActionChains(browser).move_to_element_with_offset(vertify_img_el, xoffset=x, yoffset=y).click().perform()

# 输入用户名密码
browser.find_element_by_xpath('//*[@id="J-userName"]').send_keys(user_name)
browser.find_element_by_xpath('//*[@id="J-password"]').send_keys(pass_word)
# 点击登录
browser.find_element_by_xpath('//*[@id="J-login"]').click()
# 滑动滑块
sleep(1)
btn = browser.find_element_by_xpath('//*[@id="nc_1_n1z"]')
# 水平拉动距离300px
ActionChains(browser).drag_and_drop_by_offset(btn, xoffset=300, yoffset=0).perform()

